package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程管理控制器
 */
@RestController
@RequestMapping("/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 条件分页查询课程列表
     */
    @GetMapping
    public Result list(String name, Integer subject, Integer target, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult<Course> coursePage = courseService.list(name, subject, target, page, pageSize);
        return Result.success(coursePage);
    }

    /**
     * 新增课程
     */
    @PostMapping
    public Result add(@RequestBody Course course) {
        courseService.add(course);
        return Result.success();
    }

    /**
     * 更新课程信息
     */
    @PutMapping
    public Result update(@RequestBody Course course) {
        courseService.update(course);
        return Result.success();
    }

    /**
     * 根据ID查询课程信息
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Course course = courseService.getById(id);
        return Result.success(course);
    }

    /**
     * 根据ID删除课程信息
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        courseService.delete(id);
        return Result.success();
    }

    /**
     * 根据学科查询课程列表
     */
    @GetMapping("/subject/{subject}")
    public Result getCoursesBySubject(@PathVariable Integer subject) {
        List<Course> courses = courseService.getCoursesBySubject(subject);
        return Result.success(courses);
    }

}